package com.rapid.maintenance.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.trello.rxlifecycle2.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author by yu.wang
 * User For
 * create time 2018/12/28
 */

public abstract class BaseFragment extends RxFragment {
    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (getArguments() != null) {
            initArguments(getArguments());
        }
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        if (unbinder != null) unbinder.unbind();
        super.onDestroyView();
    }


    protected boolean isUseEventBus() {
        return false;
    }

    protected void initArguments(Bundle arguments) {

    }


    @Override
    public void onDestroy() {
        if (isUseEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            onHide();
        } else {
            onShow();
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onResume() {
        LogUtils.e(this.getClass().getSimpleName() + " onResume");
        super.onResume();
    }

    protected void onHide() {
        LogUtils.e(this.getClass().getSimpleName() + " onHide");
    }

    protected void onShow() {
        LogUtils.e(this.getClass().getSimpleName() + " onShow");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            onVisible();
        } else {
            onUnVisible();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    protected void onUnVisible() {
        LogUtils.e(this.getClass().getSimpleName() + " onUnVisible");
    }

    protected void onVisible() {
        LogUtils.e(this.getClass().getSimpleName() + " visible");

    }

    /**
     * 页面跳转
     *
     * @param clz activity.class
     */
    public void startActivity(Class<? extends Activity> clz) {
        startActivity(clz, new Intent());
    }

    public void startActivity(Class<? extends Activity> clz, int requestCode) {
        Intent intent = new Intent(requireContext(), clz);
        startActivityForResult(intent, requestCode);
    }


    /**
     * 携带数据的页面跳转
     *
     * @param clz    activity.class
     * @param intent data
     */
    public void startActivity(Class<? extends Activity> clz, @NonNull Intent intent) {
        intent.setClass(requireContext(), clz);
        startActivity(intent);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(provideRootLayout(), container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    /**
     * 初始化数据及交互
     */
    protected abstract void init();

    /**
     * @return 布局id
     */
    protected abstract @LayoutRes
    int provideRootLayout();
}

